package com.yingxin.yx.framework.web;

import com.yingxin.yx.framework.commons.constant.PathConst;
import com.yingxin.yx.framework.commons.helper.PropertiesHelper;
import com.yingxin.yx.framework.web.converter.GlobalDateConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author fanchao
 */
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

        // 上传
        String uploadDir = PropertiesHelper.getInstance().getProperty(PathConst.UPLOAD_DIR);
        String uploadPath = "file:" + uploadDir + "/upload/";
        registry.addResourceHandler("/upload/**").addResourceLocations(uploadPath);

        // 企业微信附件素材
        String wecomDir = PropertiesHelper.getInstance().getProperty(PathConst.UPLOAD_DIR);
        String wecomPath = "file:" + wecomDir + "/wecom/";
        registry.addResourceHandler("/wecom/**").addResourceLocations(wecomPath);

        // 图片
        String reportDir = PropertiesHelper.getInstance().getProperty(PathConst.REPORT_DIR);
        String reportPath = "file:" + reportDir + "/Image/";
        registry.addResourceHandler("/Image/**").addResourceLocations(reportPath);

        // pdf
        String reportPathPdf = "file:" + reportDir + "/Pdf/";
        registry.addResourceHandler("/Pdf/**").addResourceLocations(reportPathPdf);

        // App
        String appDir = PropertiesHelper.getInstance().getProperty(PathConst.EXPORT_APP);
        String appPath = "file:" + appDir + "/app/";
        registry.addResourceHandler("/app/**").addResourceLocations(appPath);

        // excel 导出
        String exportDir = PropertiesHelper.getInstance().getProperty(PathConst.EXPORT_DIR);
        String excelPath = "file:" + exportDir + "/excel/";
        registry.addResourceHandler("/excel/**").addResourceLocations(excelPath);

        // 静态文件
        String staticDir = PropertiesHelper.getInstance().getProperty(PathConst.STATIC_DIR);
        String staticPath = "file:" + staticDir + "/static/";
        registry.addResourceHandler("/static/**").addResourceLocations(staticPath);

        // 下载
        String downloadDir = PropertiesHelper.getInstance().getProperty(PathConst.DOWNLOAD_DIR);
        String downloadPath = "file:" + downloadDir + "/download/";
        registry.addResourceHandler("/download/**").addResourceLocations(downloadPath);
        super.addResourceHandlers(registry);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
        super.addViewControllers(registry);
    }


    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new GlobalDateConverters());
        super.addFormatters(registry);
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        // registry.addInterceptor(new DefaultHandlerInterceptor()).addPathPatterns("/**");
        //.excludePathPatterns("/UserCon/**", "/Doctor/**", "/SMS/**");
        // 证书验证
        // registry.addInterceptor(this.getLicenseCheckInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}
